import { h } from 'vue';
import { NAvatar, NTag, NButton, NIcon } from 'naive-ui';
import { BasicColumn } from '@/components/Table';
import dayjs from 'dayjs';

// 二手物品结构类型定义
export interface SecondHandItem {
  itemID: number;
  sellerID: number;
  title: string;
  description: string;
  price: number;
  creationTime: string;
  status: 'forSale' | 'inTransaction' | 'sold';
}

// 表格列定义
export const secondHandColumns: BasicColumn<SecondHandItem>[] = [
  {
    title: '物品ID',
    key: 'itemID',
    width: 80,
  },
  {
    title: '卖家ID',
    key: 'sellerID',
    width: 80,
  },
  {
    title: '标题',
    key: 'title',
    width: 200,
  },
  {
    title: '价格',
    key: 'price',
    width: 100,
    render(record) {
      return h('span', {}, `￥${record.price}`);
    },
  },
  {
    title: '创建时间',
    key: 'creationTime',
    width: 180,
    render(record) {
      // 使用 dayjs 进行日期格式化
      const formattedDate = dayjs(record.creationTime).format('YYYY-MM-DD HH:mm:ss');
      return h('span', {}, formattedDate);
    },
  },
  {
    title: '状态',
    key: 'status',
    width: 100,
    render(record) {
      const statusMap = {
        forSale: { type: 'success', label: '在售' },
        sold: { type: 'error', label: '已售出' },
        inTransaction: { type: 'warning', label: '交易中' }
      };
      
      const status = statusMap[record.status] || { type: 'default', label: record.status };
      return h(NTag, { type: status.type as 'success' | 'error' | 'warning' | 'default' }, { default: () => status.label });
    },
  },
];

// 示例处理函数（你需要根据实际情况实现这些函数）
function handleEdit(item: SecondHandItem) {
  console.log('Editing item:', item);
  // 实现编辑逻辑
}

function handleDelete(item: SecondHandItem) {
  console.log('Deleting item:', item);
  // 实现删除逻辑
}
